package com.chunuo.eduservice.controller;


import com.chunuo.commonutils.R;
import com.chunuo.eduservice.entity.EduSubject;
import com.chunuo.eduservice.entity.vo.SubjectVo;
import com.chunuo.eduservice.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@RestController
@RequestMapping("/eduservice/edu-subject")
//@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService eduSubjectService;

    /**
     * 找到树节点
     * @return
     */
    @GetMapping("findTreeSubject")
    public R findTreeSubject(){
        List<SubjectVo> list = eduSubjectService.findTreeSubject();
        return R.ok().data("data",list);
    }


    /**
     * excel保存课程
     * @param file
     * @return
     */
    @PostMapping("/saveSubject")
    public R saveSubject(MultipartFile file){
        if(file!=null){
            eduSubjectService.saveSubject(file,eduSubjectService);
        }
        return R.ok();
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id")String id){
        EduSubject subject = eduSubjectService.getById(id);
        return R.ok().data("info",subject);
    }

    @PostMapping("/save")
    public R save(@RequestBody EduSubject subject){
        eduSubjectService.save(subject);
        return R.ok().data("message","课程添加成功！");
    }

    @PutMapping("/edit")
    public R edit(@RequestBody List<EduSubject> subjects){
        eduSubjectService.updateBatchById(subjects);
        return R.ok().data("message","课程修改成功！");
    }


    @DeleteMapping("/remove/batch")
    public R delete(@RequestBody List<String> ids){
        eduSubjectService.removeByIds(ids);
        return R.ok();
    }

}

